﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DuBaiCoffee2.Model
{
    public class RobotsMsg
    {
        public class IOMsg
        {
            private static IOMsg instance;
            private IOMsg() { }

            public static IOMsg Instance
            {
                get
                {
                    if (instance == null)
                    {
                        instance = new IOMsg();
                    }
                    return instance;
                }
            }
            #region IO通讯
            public bool AllBol = true;
            public bool RobotBol = true;
            public delegate void IOdelegate(string btnName, string tag);
            public IOdelegate iodelegate;
            /// <summary>
            ///  IO的通讯返回数据
            /// </summary>
            private List<byte> buffer = new List<byte>();
            public static bool IsMsgError = true;
            string nostrs = "";
            public Thread All_thr;
            public void QK()
            {
                try
                {
                    //IOMsg.Instance.AllBol = false;
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFJ_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJB1_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJB2_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JCJB_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JGT_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_DYWC_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDY_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDY_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KFWC_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JBCG_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QJKF_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_KB_Down);
                    //Thread.Sleep(100);
                    //IOMsg.Instance.AllBol = true;
                }
                catch (Exception)
                {
                    return;
                }
            }
            public void IOSerial_Thr()
            {
                try
                {
                    var ddd = SerialPort.GetPortNames().ToList();
                    if (!SerialPort.GetPortNames().ToList().Any(a => a.ToLower().Contains(IPorPortInfo.Instance.PortName)))
                    {
                        Thread.Sleep(1000);
                        IOSerial_Thr();
                    }
                    IOSerialPort.Instance.IceMachineConnect(IPorPortInfo.Instance.PortName, 38400, "", 8, 1);//其他串口通讯
                    IOSerialPort.Instance.serialPort.DataReceived += SerialPort_DataReceived;
                    Robot_Thr();
                    All_thr = new Thread(AllMessages);
                    All_thr.IsBackground = true;
                    All_thr.Start();
                }
                catch (Exception)
                {
                    IOSerialPort.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
                    Thread.Sleep(100);
                    IOSerial_Thr();
                }
            }
            public void Robot_Thr()
            {
                try
                {
                    // RobotOperate.Read_CSV(); //读取轨迹文件
                    RobotOperate.Instance.Robot_Initialisation_double(); //机器人初始化
                }
                catch (Exception)
                {
                    Thread.Sleep(100);
                    Robot_Thr();
                }
            }
            /// <summary>
            /// PING 通 IP
            /// </summary>
            /// <param name="IP"></param>
            /// <returns></returns>
            public bool PingIP(string IP)
            {
                try
                {
                    if (IP.Split('.').Count() < 4) return false;
                    Ping pingSender = new Ping();
                    PingReply reply = pingSender.Send(IP, 120);//第一个参数为ip地址，第二个参数为ping的时间
                    if (reply.Status == IPStatus.Success)
                    {
                        return true;
                    }
                    else
                    {
                        //ping不通
                        return false;
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }
            public void Msg(string btnName, string tag)
            {
                try
                {
                    //IOMsg.Instance.AllBol = false;
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LOpenBulkPowerDown);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LCloseBulkPowerDown);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_PTBPE_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_XSN_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDCX_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_Stop_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_ZTCX_Down);
                    //Thread.Sleep(100);
                    //IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JXYXCX_Down);
                    //Thread.Sleep(100);
                    //switch (btnName)
                    //{
                    //    case "DO1":
                    //        if (tag == "0")
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_start);
                    //        else
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_UP_end);
                    //        break;
                    //    case "DO2":
                    //        if (tag == "0")
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_start);
                    //        else
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.PushRod_Down_end);
                    //        break;
                    //    case "DO3":
                    //        if (tag == "0")
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_start);
                    //        else
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction1_end);
                    //        break;
                    //    case "DO4":
                    //        if (tag == "0")
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction2_start);
                    //        else
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.BSG_Direction2_end);
                    //        break;
                    //    case "DO5":
                    //        if (tag == "0")
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Fructose1_start);
                    //        else
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Fructose1_end);
                    //        break;
                    //    case "DO6":
                    //        if (tag == "0")
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Fructose2_start);
                    //        else
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Fructose2_end);
                    //        break;
                    //    case "DO7":
                    //        if (tag == "0")
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Fructose3_start);
                    //        else
                    //            IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.Fructose3_end);
                    //        break;
                    //    case "DO8":
                    //        IPorPortInfo.Instance.SweetInit1 = Convert.ToInt32(tag);
                    //        using (FileStream stream = File.Create(MainWindow.IPorPortUrl))
                    //        {
                    //            stream.Close();
                    //            stream.Dispose();
                    //            IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                    //            File.WriteAllText(MainWindow.IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    //        }
                    //        break;
                    //    case "DO9":
                    //        IPorPortInfo.Instance.SweetInit2 = Convert.ToInt32(tag);
                    //        using (FileStream stream = File.Create(MainWindow.IPorPortUrl))
                    //        {
                    //            stream.Close();
                    //            stream.Dispose();
                    //            IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                    //            File.WriteAllText(MainWindow.IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    //        }
                    //        break;
                    //    case "DO10":
                    //        IPorPortInfo.Instance.SweetInit3 = Convert.ToInt32(tag);
                    //        using (FileStream stream = File.Create(MainWindow.IPorPortUrl))
                    //        {
                    //            stream.Close();
                    //            stream.Dispose();
                    //            IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                    //            File.WriteAllText(MainWindow.IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    //        }
                    //        break;
                    //    case "DO11":
                    //        Thread.Sleep(100);
                    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn1_start);
                    //        DigitalMsgBol.Instance.RobotPowerOn1_Bol = true;
                    //        Thread.Sleep(100);
                    //        while (true)
                    //        {
                    //            RobotOperate.CX_kj();
                    //            if (RobotOperate.state.poweredOn == 1)
                    //            {
                    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.RobotPowerOn1_end);
                    //                Thread.Sleep(100);
                    //                break;
                    //            }
                    //        }
                    //        RobotMsg(true, false, false, false, false, false, false);
                    //        break;
                    //    case "DO12":
                    //        Thread.Sleep(100);
                    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_BJ);
                    //        Thread.Sleep(100);
                    //        RobotMsg(false, false, false, false, false, false, true);
                    //        break;
                    //    case "DO13":
                    //        Thread.Sleep(100);
                    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LOpenBulkPowerUP);
                    //        Thread.Sleep(100);
                    //        while (true)
                    //        {
                    //            RobotOperate.CX_status();
                    //            if (RobotOperate.status.powered_on == 1)
                    //            {
                    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LOpenBulkPowerDown);
                    //                Thread.Sleep(100);
                    //                break;
                    //            }
                    //        }
                    //        RobotMsg(false, true, false, false, false, false, false);
                    //        break;
                    //    case "DO14":
                    //        Thread.Sleep(100);
                    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LCloseBulkPowerUP);
                    //        Thread.Sleep(100);
                    //        RobotBol = true;
                    //        while (true)
                    //        {
                    //            RobotOperate.CX_status();
                    //            if (RobotOperate.status.powered_on == 0)
                    //            {
                    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LCloseBulkPowerDown);
                    //                Thread.Sleep(100);
                    //                break;
                    //            }
                    //        }
                    //        RobotMsg(true, false, false, false, false, false, false);
                    //        break;
                    //    case "DO15":
                    //        Thread.Sleep(100);
                    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_PTBPE_UP);
                    //        Thread.Sleep(100);
                    //        RobotBol = false;
                    //        while (true)
                    //        {
                    //            RobotOperate.CX_status();
                    //            if (RobotOperate.status.enabled == 1)
                    //            {
                    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_PTBPE_Down);
                    //                Thread.Sleep(100);
                    //                break;
                    //            }
                    //        }
                    //        RobotMsg(false, false, true, false, false, false, false);
                    //        break;
                    //    case "DO16":
                    //        Thread.Sleep(100);
                    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_XSN_UP);
                    //        Thread.Sleep(100);
                    //        RobotBol = true;
                    //        while (true)
                    //        {
                    //            RobotOperate.CX_status();
                    //            if (RobotOperate.status.enabled == 0)
                    //            {
                    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_XSN_Down);
                    //                Thread.Sleep(100);
                    //                break;
                    //            }
                    //        }
                    //        RobotMsg(false, true, false, false, false, false, false);
                    //        break;
                    //    case "DO17":
                    //        Thread.Sleep(100);
                    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDCX_UP);
                    //        Thread.Sleep(100);
                    //        while (true)
                    //        {
                    //            RobotOperate.CX_Start();
                    //            if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                    //            {
                    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_QDCX_Down);
                    //                Thread.Sleep(100);
                    //                break;
                    //            }
                    //        }
                    //        RobotMsg(false, false, false, true, false, false, false);
                    //        break;
                    //    case "DO18":
                    //        Thread.Sleep(100);
                    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_Stop_UP);
                    //        Thread.Sleep(100);
                    //        while (true)
                    //        {
                    //            RobotOperate.CX_Start();
                    //            if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_IDLE)
                    //            {
                    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_Stop_Down);
                    //                Thread.Sleep(100);
                    //                break;
                    //            }
                    //        }
                    //        RobotMsg(false, false, false, false, true, false, false);
                    //        break;
                    //    case "DO19":
                    //        Thread.Sleep(100);
                    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_ZTCX_UP);
                    //        DigitalMsgBol.Instance.RobotPowerOn1_Bol = true;
                    //        Thread.Sleep(100);
                    //        RobotBol = false;
                    //        while (true)
                    //        {
                    //            RobotOperate.CX_Start();
                    //            if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_PAUSED)
                    //            {
                    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_ZTCX_Down);
                    //                Thread.Sleep(100);
                    //                break;
                    //            }
                    //        }
                    //        RobotMsg(false, false, false, false, false, true, false);
                    //        break;
                    //    case "DO20":
                    //        Thread.Sleep(100);
                    //        IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JXYXCX_UP);
                    //        DigitalMsgBol.Instance.RobotPowerOn1_Bol = true;
                    //        Thread.Sleep(100);
                    //        RobotBol = false;
                    //        while (true)
                    //        {
                    //            RobotOperate.CX_Start();
                    //            if (RobotOperate.pstatus == JKTYPE.ProgramState.PROGRAM_RUNNING)
                    //            {
                    //                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.LRobot_JXYXCX_Down);
                    //                Thread.Sleep(100);
                    //                break;
                    //            }
                    //        }
                    //        RobotMsg(false, false, false, true, false, false, false);
                    //        break;
                    //    default:
                    //        break;
                    //}
                    //iodelegate(btnName, tag == "1" ? "0" : "1");
                    //Thread.Sleep(100);
                    //IOMsg.Instance.AllBol = true;
                }
                catch (Exception)
                {
                    return;
                }
            }

            public void AllMessages()
            {
                try
                {
                    while (true)
                    {
                        try
                        {
                            if (Instance.AllBol)
                                IOSerialPort.Instance.SerialSend(DigitalMsg.Instance.AllMessage);
                            Thread.Sleep(100);
                        }
                        catch (Exception)
                        {
                            Thread.Sleep(100);
                        }

                    }
                }
                catch (Exception)
                {
                    IOSerialPort.Instance.serialPort.DataReceived -= SerialPort_DataReceived;
                    Thread.Sleep(100);
                    IOSerial_Thr();
                }
            }
            private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                try
                {
                    byte[] result = new byte[IOSerialPort.Instance.serialPort.BytesToRead];
                    IOSerialPort.Instance.serialPort.Read(result, 0, IOSerialPort.Instance.serialPort.BytesToRead);
                    buffer.AddRange(result);
                    string str = "";// $"{DateTime.Now}：\n";
                                    //判断读写模式
                    if (buffer.Count > 0x03)
                        str += $"{Encoding.ASCII.GetString(result)}";
                    for (int i = 0; i < result.Length; i++)
                    {
                        str += $"{result[i].ToString("X2")} ";
                    }
                    MsgShow(str);
                    Thread.Sleep(1);
                }
                catch (Exception)
                {
                    return;
                }
            }
            public void DigitalMsgBol2(string str)
            {
                try
                {

                    var Bol = str.Split(' ');
                    bool bol = false;
                    bool isEmail = false;
                    if (WuLiaoCoffeeSel.Instance.BANNER_Sel_Bol != (Bol[4] == "01" ? true : false))
                    {
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.BANNER_Sel_Bol = Bol[4] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol != (Bol[10] == "01" ? true : false))
                    {
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.OutMealCup_Sel_Bol = Bol[10] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.PushRodUP_Sel_Bol != (Bol[6] == "01" ? true : false))
                    {
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.PushRodUP_Sel_Bol = Bol[6] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.PushRodDown_Sel_Bol != (Bol[8] == "01" ? true : false))
                    {
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.PushRodDown_Sel_Bol = Bol[8] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol != (Bol[12] == "01" ? true : false))
                    {
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.TakeCup_Sel_Bol = Bol[12] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.LBQFK1_Bol != (Bol[14] == "01" ? true : false))
                    {
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.LBQFK1_Bol = Bol[14] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.LBQFK2_Bol != (Bol[16] == "01" ? true : false))
                    {
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.LBQFK2_Bol = Bol[16] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol != (Bol[22] == "01" ? true : false))
                    {
                        if (WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol)
                            isEmail = true;
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.CupFeed1_Sel_Bol = Bol[22] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol != (Bol[24] == "01" ? true : false))
                    {
                        if (WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol)
                            isEmail = true;
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.CupFeed2_Sel_Bol = Bol[24] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.Milk_Sel_Bol != (Bol[18] == "01" ? true : false))
                    {
                        if (WuLiaoCoffeeSel.Instance.Milk_Sel_Bol)
                            isEmail = true;
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.Milk_Sel_Bol = Bol[18] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.Water_Sel_Bol != (Bol[20] == "01" ? true : false))
                    {
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.Water_Sel_Bol = Bol[20] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.GT1_Sel_Bol != (Bol[26] == "01" ? true : false))
                    {
                        if (WuLiaoCoffeeSel.Instance.GT1_Sel_Bol)
                            isEmail = true;
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.GT1_Sel_Bol = Bol[26] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.GT2_Sel_Bol != (Bol[28] == "01" ? true : false))
                    {
                        if (WuLiaoCoffeeSel.Instance.GT2_Sel_Bol)
                            isEmail = true;
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.GT2_Sel_Bol = Bol[28] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.GT3_Sel_Bol != (Bol[30] == "01" ? true : false))
                    {
                        if (WuLiaoCoffeeSel.Instance.GT3_Sel_Bol)
                            isEmail = true;
                        bol = true;
                    }
                    WuLiaoCoffeeSel.Instance.GT3_Sel_Bol = Bol[30] == "01" ? true : false;
                    if (WuLiaoCoffeeSel.Instance.YBJ_Water != (Bol[32] == "01" ? true : false))
                    {
                        if (WuLiaoCoffeeSel.Instance.YBJ_Water)
                            isEmail = true;
                    }
                    WuLiaoCoffeeSel.Instance.YBJ_Water = Bol[32] == "01" ? true : false;
                    if (isEmail)
                    {
                        TestDelegateInvoke testDelegate = new TestDelegateInvoke(DelegateInvokeMethod);
                        testDelegate.BeginInvoke(null, null);
                    }
                    if (bol)
                    {
                        ClientList clientList = new ClientList();
                        clientList.MsgBol = WuLiaoCoffeeSel.Instance;
                        clientList.code = 2;
                        SocketClients.Instance.client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(clientList)));
                    }
                }
                catch (Exception)
                {
                    return;
                }
            }
            public delegate void TestDelegateInvoke();

            private void DelegateInvokeMethod()
            {
                try
                {
                    EmailShow emailShow = new EmailShow();
                    emailShow.content = MainWindow.EmailError();
                    var saves1 = JsonConvert.SerializeObject(emailShow);
                    Console.WriteLine(emailShow.content);
                    var dd1 = HttpMsg.Instance.sendEmailMsg(saves1);
                }
                catch (Exception)
                {
                    return;
                }
            }
            public void MsgShow(string str)
            {
                try
                {
                    //Console.WriteLine(str);
                    int count = str.Length / 315;
                    if (count > 0)
                    {
                        var str1 = str.Substring(str.Length - count * 315, 315);
                        //Console.WriteLine(str1);
                        if (str1.Split(' ').Count() == 105)
                        {
                            DigitalMsgBol2(str1);
                            if (count > 1)
                                MsgShow(str.Substring(str.Length - (count - 1) * 315, (count - 1) * 315));
                        }
                        else
                        {
                            var nostr = str1.Split(' ');
                            int cc = int.TryParse(nostr[0].Substring(nostr[0].Length - 2, 2), out int a) ? a : -1;
                            if (cc != -1)
                            {
                                nostrs += nostr[0].Substring(nostr[0].Length - 2, 2) + " ";
                                for (int i = 1; i < nostr.Count() - 1; i++)
                                {
                                    nostrs += nostr[i] + " ";
                                }
                                // Console.WriteLine(nostrs);
                                if (nostrs.Substring(0, 5) == "02 04")
                                {
                                    if (nostrs.Count() >= 315)
                                    {
                                        DigitalMsgBol2(nostrs);
                                        nostrs = "";
                                    }
                                }
                                else
                                {
                                    nostrs = "";
                                }
                            }
                        }
                    }
                    else
                    {

                        var nostr = str.Split(' ');
                        nostrs += nostr[0].Substring(nostr[0].Length - 2, 2) + " ";
                        for (int i = 1; i < nostr.Count() - 1; i++)
                        {
                            nostrs += nostr[i] + " ";
                        }
                        // Console.WriteLine(nostrs);
                        if (nostrs.Substring(0, 5) == "02 04")
                        {
                            if (nostrs.Count() >= 315)
                            {
                                DigitalMsgBol2(nostrs);
                                nostrs = "";
                            }
                        }
                        else
                        {
                            if (nostrs.Substring(0, 5) == "01 05")
                            {
                                Console.WriteLine(str);
                            }
                            else if (nostrs.Substring(0, 5) == "02 04")
                            {
                                Console.WriteLine(nostrs);
                                if (nostrs.Contains("02 04 02 00 01 3C F0"))
                                {
                                    //  DigitalMsgBol.Instance.JBCGJC_Bol = true;
                                }
                            }
                            else if (nostrs.Substring(0, 5) == "03 02")
                            {
                                Console.WriteLine(nostrs);
                                var robstr = nostrs.Split(' ');
                                RobotBol = robstr[3] == "01" ? true : false;
                                Console.WriteLine(robstr[3] == "01" ? true : false);
                            }
                            else if (nostrs.Substring(0, 5) == "04 02")
                            {
                                Console.WriteLine(nostrs);
                                var robstr = nostrs.Split(' ');
                                RobotBol = robstr[3] == "01" ? true : false;
                                Console.WriteLine(robstr[3] == "01" ? true : false);
                            }
                            nostrs = "";
                        }
                    }

                }
                catch (Exception)
                {
                    return;
                }
            }
            public class EmailShow
            {
                public string title { get; set; } = "Shortage warning";
                public int deviceId { get; set; } = 1;
                public string content { get; set; } = "";
                public string fromname { get; set; } = "Coffee Shop";
            }
            #endregion
        }
    }
}
